package com.niit.controller;


import com.niit.entity.Cinema;
import com.niit.service.CinemaService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin(origins = "http://domain2.com", maxAge = 3600)
@RestController
@RequestMapping("/Cinema")
public class CinemaController {

    @Resource
    private CinemaService CinemaService;

    /**
     * 查询所有电影信息
     * @param Movie 电影查询条件
     * @return 电影列表信息
     */

    @RequestMapping("/findAllCinemas")
    public List<Cinema> findAllCinemas() {
        return CinemaService.findAllCinemas();
    }

    @GetMapping("/findCinemaBy/{id}")
    public Cinema queryById(@PathVariable("id") Integer id) {
        return this.CinemaService.findCinemaById(Long.valueOf(id));
    }

    @RequestMapping("/findCinemaByName")
    public Cinema findCinemaByCinema(@RequestBody Cinema cinema) {
        System.out.println("前端发送的数据:"+cinema);
        String cinema_name = cinema.getCinemaName();
        Cinema dbCinema  = this.CinemaService.findCinemaByName(cinema_name);
        if (dbCinema != null) {
            return dbCinema;
        }
        return dbCinema;
    }

    @PostMapping("/addCinema")
    public Cinema addCinema(@RequestBody Cinema cinema) {
        System.out.println("前端发送的数据:"+cinema);
        return this.CinemaService.addCinema(cinema);
    }

    @RequestMapping("/updateCinema")
    public String updateMovie(@RequestBody Cinema cinema) {
        System.out.println("前端发送的数据:"+cinema);
        int result = CinemaService.updateCinema(cinema);
        if (result >= 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    /*@RequestMapping("/deleteMovie/{id}")*/
    @RequestMapping("/deleteCinema")
    public String deleteMovie(@RequestBody Cinema cinema) {
        System.out.println("前端发送的数据:"+cinema);
        Long id = cinema.getCinemaId();
        System.out.println("前端发送数据id:"+id);
        Cinema dbCinemaID = this.CinemaService.findCinemaById(id);
        System.out.println("数据库获取的id:"+dbCinemaID);
        if (dbCinemaID == null) {
            // 处理id为null的情况，比如返回错误信息或者重定向到一个错误页面
            return "请求的ID不存在";
        }
        int result = CinemaService.deleteCinema(Math.toIntExact(dbCinemaID.getCinemaId()));
        if(result >= 1 )
        {
            return "删除成功";
        }
        else
        {
            return "删除失败";
        }
    }







}
